package com.medical.literature.common.vo;

import lombok.Data;
import lombok.NoArgsConstructor;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonCreator;

/**
 * 统一响应结果类
 */
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result<T> {
    private int code;
    private String message;
    private T data;

    @JsonCreator
    public static <T> Result<T> of(@JsonProperty("code") int code, 
                                  @JsonProperty("message") String message, 
                                  @JsonProperty("data") T data) {
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        result.data = data;
        return result;
    }

    public static <T> Result<T> success() {
        return of(200, "操作成功", (T) null);
    }

    public static <T> Result<T> success(T data) {
        return of(200, "操作成功", data);
    }

    public static <T> Result<T> success(String message, T data) {
        return of(200, message, data);
    }

    public static <T> Result<T> error() {
        return of(500, "操作失败", (T) null);
    }

    public static <T> Result<T> error(String message) {
        return of(500, message, (T) null);
    }

    public static <T> Result<T> error(int code, String message) {
        return of(code, message, (T) null);
    }

    public boolean isSuccess() {
        return this.code == 200;
    }
}